package com.bwhx.eruler.controller.casdilly;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Baofei;
import com.bwhx.eruler.bean.Buyer;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.FenchangStatus;
import com.bwhx.eruler.bean.casdilly.Linkman;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.LinkmanService;
import com.bwhx.eruler.util.Constant;
import com.bwhx.eruler.util.StringUtils;


@Controller
@RequestMapping("/linkman")
public class LinkmanController extends BaseController{
	
	@Autowired
	LinkmanService linkmanService;
	
	
	
	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	 public Map<String,Object> linkmanList(Integer page,Integer rows,Linkman linkman,HttpSession session) {
		
		Staff staff  = (Staff) session.getAttribute("staff");
		if(null==staff.getUserType()||1!=staff.getUserType()){
			linkman.setCreatorID(staff.getsId().toString());

		}
		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,linkman);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<Linkman> list = linkmanService.list(p);
			Integer count = linkmanService.count(linkman);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
 			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	
 	@ResponseBody
	@RequestMapping("/page.everyone.findAllByContractSerial.ashx")
	public List<Linkman> findAllByContractSerial(Linkman lm){
 		
		 
			List<Linkman> linkmanList = linkmanService.findByContractSerial(lm.getlMIDContract());
			 
		
		return linkmanList;
		
	}
	
	
	@Deprecated
	@ResponseBody
	@RequestMapping("/findByContractSerial.ashx")
	public HashMap<String,Object> findByContractSerial(HttpServletRequest request){
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		try {
			List<Linkman> linkmanList = linkmanService.findByContractSerial((String) request.getAttribute("cPIDSerial"));
			resultMap.put(Constant.DATA, linkmanList);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}
		
		
		return resultMap;
		
	}
	
	@ResponseBody
	@RequestMapping("/findByContractSerialAndCreatorID.ashx")
	public HashMap<String,Object> findByContractSerialAndCreatorID(HttpServletRequest request){
		HashMap<String,Object> resultMap = new HashMap<String,Object>();
		
		String cPIDSerial = (String)request.getAttribute("cPIDSerial");
		String creatorID = (String)request.getAttribute("creatorID");
		String pageNumberstr = (String) request.getAttribute("page_number");
		
		if(StringUtils.isEmpty(pageNumberstr)){
			resultMap.put(Constant.STATUS, "0");
			resultMap.put(Constant.MSG, "页数不能为空");
			return resultMap;
		}
		
		try {
			
			Integer pageNumber = new Integer(pageNumberstr);
			int start = (pageNumber-1)*10;
			List<Linkman> linkmanList = linkmanService.findByContractSerialAndCreatorID(cPIDSerial,creatorID,start );
			resultMap.put(Constant.DATA, linkmanList);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.MSG, Constant.MSG_SUCCESS);
		} catch (Exception e) {
			log.error(getTrace(e));
			resultMap.put(Constant.STATUS, Constant.STATUS_FAIL);
			resultMap.put(Constant.MSG, Constant.MSG_FAIL);
		}
		
		
		return resultMap;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateFenchang.ashx")
	public String saveOrUpdateFenchang(Linkman lm) {
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			List<Linkman> resultLinkMan = linkmanService.findByNameAndContractSerial(lm);
			
			if(lm.getlMIDFenChang().equals("-1")){
				lm.setCreatorID(sessionUser.getsId().toString());
				lm.setCreateTime(new Date());
				
				if(resultLinkMan.size()>0){
					return "conflict";
				}else{
					linkmanService.insert(lm);
				}
				
				
			}else{
				
				lm.setModifierID (sessionUser.getsId().toString());
				lm.setModifyTime(new Date());
				
				if(resultLinkMan.size()==0 || (resultLinkMan.size()==1&&resultLinkMan.get(0).getlMIDFenChang().equals(lm.getlMIDFenChang()))){
					linkmanService.update(lm);
				}else{
					return "conflict";	
				}
				

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
	@RequestMapping(value = "/page.delete.ashx")
	public String delete(Linkman lm) {
		
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			
  			lm.setModifierID(sessionUser.getsId().toString());
 			
			linkmanService.delete(lm);
		 
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.linkmanStatusList.ashx")
	 public Map<String,Object> linkmanStatusList(Integer page,Integer rows,Linkman linkman,HttpSession session) {
		
		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,linkman);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<FenchangStatus> list = linkmanService.linkmanStatusList(p);
			
			for (FenchangStatus fenchangStatus : list) {
				fenchangStatus.setFenchangCustomerProgressRatio(fenchangStatus.getFenchangDoneCustomer()+"/"+fenchangStatus.getFenchangTotalCustomer());
			}
			Integer count = linkmanService.linkmanStatusCount(linkman);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }	
	
	

}
